<?php

class DeliveryOrderTicketController extends BackendApplicationController {
	public $layout = 'application.modules.delivery.views.layouts.delivery';

	public function actionView($id) {
		$moveDetail = new DeliveryMoveDetailModel;
		$updateTour = Yii::app()->request->getParam('ot',0);
		if($updateTour==1){
			//
			$criteria = new CDbCriteria;
			$criteria->condition = "order_ticket_id=:ot";
			$criteria->params = array(':ot'=>$id);
			$orderTour = DeliveryMoveDetailModel::model()->findAll($criteria);
			$data['orderTour']=$orderTour;
		}
		if(isset($_POST['DeliveryMoveDetailModel'])){
			$moveDetail->attributes = $_POST['DeliveryMoveDetailModel'];
			$moveDetail->user_id = Yii::app()->user->id;
			$moveDetail->created_datetime = date('Y-m-d H:i:s');
			$moveDetail->updated_datetime = date('Y-m-d H:i:s');
			//update move success
			if($_POST['DeliveryMoveDetailModel']['status']==3){
				$oderTicket = DeliveryOrderTicketModel::model()->findByPk($id);
				$oderTicket->status = DeliveryOrderTicketModel::STATUS_2;
				$res = $oderTicket->save();
			}
			if(isset($_POST['land_office']) && $_POST['land_office']>0){
				$moveDetail->office_next = $_POST['land_office'];
			}
			if(isset($_POST['reason']) && $_POST['reason']!=''){
				$moveDetail->reason = $_POST['reason'];
			}
			if(isset($_POST['office_user']) && $_POST['office_user']!=''){
				$moveDetail->office_user_id = $_POST['office_user'];
			}
			if($moveDetail->save()){
				Yii::app()->user->setFlash('message', 'Updated success!');
				$this->refresh();
			}else {
				Yii::app()->user->setFlash('message', 'Updated false!');
			}
		}
		$data['model']=$this->loadModel($id, 'DeliveryOrderTicketModel');
		$data['moveDetail']=$moveDetail;
		$data['updateTour']=$updateTour;
		$this->render('view', $data);
	}

	public function actionCreate() {
		$model = new DeliveryOrderTicketModel;


		if (isset($_POST['DeliveryOrderTicketModel'])) {
			$model->setAttributes($_POST['DeliveryOrderTicketModel']);
			if ($model->save()) {
				$lastId = $model->id;
				$model->setAttribute('code', 'AM'.$lastId);
				if($_POST['DeliveryOrderTicketModel']['ticket_type']>0){
					$model->setAttribute('air_bill', $_POST['DeliveryOrderTicketModel']['code']);
				}else{
					$model->setAttribute('air_bill', 'AM'.$lastId);
				}
				$model->save();
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->id));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'DeliveryOrderTicketModel');


		if (isset($_POST['DeliveryOrderTicketModel'])) {
			$model->setAttributes($_POST['DeliveryOrderTicketModel']);
			$model->setAttribute('code', 'AM'.$id);
			if($_POST['DeliveryOrderTicketModel']['ticket_type']>0){
				$model->setAttribute('air_bill', $_POST['DeliveryOrderTicketModel']['code']);
			}else{
				$model->setAttribute('air_bill', 'AM'.$id);
			}
			
			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->id));
			}
		}
		$model->start_datetime = date('Y-m-d', strtotime($model->start_datetime)); 
		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'DeliveryOrderTicketModel')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('DeliveryOrderTicketModel');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new DeliveryOrderTicketModel('search');
		$model->unsetAttributes();
		//get Role
		$roles = Helper::getUserAssignedRoles(Yii::app()->user->id);
		//echo $roles[0]['name'];die();
		if($roles[0]['name']!='Administrator'){
			$model->setAttribute('office', Yii::app()->user->office_id);
		}
		if (isset($_GET['DeliveryOrderTicketModel']))
			$model->setAttributes($_GET['DeliveryOrderTicketModel']);

		$this->render('admin', array(
			'model' => $model,
		));
	}
	public function actionGetActionTransfer()
	{
		$type = Yii::app()->request->getParam('type',0);
		$this->renderPartial('_getActionType',array(
			'type'=>$type
		), false, false);
	}
}